package com.knowif.backend.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.knowif.backend.dto.FastAskOrderDto;
import com.knowif.backend.service.FastAskOrderService;
import com.knowif.pojo.FastAskOrder;
import com.knowif.search.SearchCondition;
import com.knowif.system.model.RestBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ：Xh
 * @date ：Created in 2019/9/9 0009 21:33
 */
@RestController
@RequestMapping("/wechat")
@Slf4j
public class FastAskOrderController {

    @Autowired
    private FastAskOrderService fastAskOrderService;
    @Autowired
    private WxPayService wxService;

    @PostMapping("/fastAskOrder/update")
    public RestBean updateOrder(@RequestBody FastAskOrder order){
        return fastAskOrderService.update(order);
    }

    @PostMapping("/fastAskOrder/prepay")
    public RestBean prepay(@RequestBody FastAskOrderDto fastAskOrderDto, HttpServletRequest request){
        return fastAskOrderService.prepay(fastAskOrderDto, request);
    }

    @PostMapping("/fastAskOrder/notify")
    public void fastAskOrderNotifyResult(@RequestBody String xmlData, HttpServletResponse response) {
        try {
            final WxPayOrderNotifyResult notifyResult = this.wxService.parseOrderNotifyResult(xmlData);
            log.info("{}", JSONObject.toJSONString(notifyResult));
            // TODO 根据自己业务场景需要构造返回对象
            fastAskOrderService.handleNotify(notifyResult);
            response.getWriter().write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@ApiOperation(value = "退款回调通知处理")
    @PostMapping("/fastAskOrder/refundNotify")
    public String fastAskOrderRefundNotify(@RequestBody String xmlData) throws WxPayException {
        final WxPayRefundNotifyResult refundNotify = this.wxService.parseRefundNotifyResult(xmlData);
        log.info("{}", JSONObject.toJSONString(refundNotify));
        // TODO 根据自己业务场景需要构造返回对象
        fastAskOrderService.handleRefundNotify(refundNotify);
        // TODO 根据自己业务场景需要构造返回对象
        return WxPayNotifyResponse.success("成功");
    }

    /**
     * 快问订单查询
     * @param searchCondition
     * @return
     */
    @PostMapping("/fastAskOrder/queryOrderDetail")
    public RestBean queryOrderDetail(@RequestBody SearchCondition searchCondition){
        try {
            return fastAskOrderService.queryOrderDetail(searchCondition);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    /**
     * 快问订单查询by id
     * @param fastAskOrderId
     * @return
     */
    @GetMapping("/fastAskOrder/queryOrderById")
    public RestBean queryOrderById(@RequestParam("fastAskOrderId") int fastAskOrderId){
        try {
            return fastAskOrderService.queryOrderById(fastAskOrderId);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }
}
